package com.gossip;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.gossip.manager.MemberManager;


public class StartupSettings {

	private int _port;
	
	private int _logLevel;
	
	private GossipSettings _gossipSettings;
	
	private ArrayList<GossipMember> _gossipMembers;
	
	private GossipMember _me;
	
	private int _forgiveFailedCount = 10;
	
	public void setForgiveFailedCount(int count)
	{
		if(count>0) _forgiveFailedCount = count;
	}
	
	public int getForgiveFailedCount()
	{
		return _forgiveFailedCount;
	}	
	
	public StartupSettings(int port, int logLevel) {
		this(port, logLevel, new GossipSettings());
	}

	public StartupSettings(int port, int logLevel, GossipSettings gossipSettings) {
		_port = port;
		_logLevel = logLevel;
		_gossipSettings = gossipSettings;
		_gossipMembers = new ArrayList<GossipMember>();
	}
	
	public void setPort(int port) {
		_port = port;
	}
	
	public int getPort() {
		return _port;
	}
	
	public void setLogLevel(int logLevel) {
		_logLevel = logLevel;
	}
	
	public int getLogLevel() {
		return _logLevel;
	}
	
	public GossipSettings getGossipSettings() {
		return _gossipSettings;
	}

	public void addGossipMember(GossipMember member) {
		_gossipMembers.add(member);
	}
	
	public ArrayList<GossipMember> getGossipMembers() {
		return _gossipMembers;
	}
	
	public static StartupSettings fromJSONFile(File jsonFile)
			throws JSONException, FileNotFoundException, IOException {
		// Read the file to a String.
		jsonFile.getPath();
		GossipService.debug("Read config from:"+jsonFile.getAbsolutePath());
		BufferedReader br = new BufferedReader(new FileReader(jsonFile));
		StringBuffer buffer = new StringBuffer();
		String line;
		while ((line = br.readLine()) != null) {
			buffer.append(line.trim());
		}

		// Lets parse the String as JSON.
		GossipService.debug("config content is:"+buffer.toString());
		JSONObject jsonObject = new JSONObject(buffer.toString());
		

		// Now get the port number.
		int port = jsonObject.getInt("port");

		int forgiveFailedCount = jsonObject.has("forgiveFailedCount") ? jsonObject
				.getInt("forgiveFailedCount") : -1;

		int gossipInterval = jsonObject.getInt("gossip_interval");

		System.out.println("Config [port: " + port +  ", gossip_interval: " + gossipInterval
				+ "forgiveFailedCount: " + forgiveFailedCount + "]");

		// Initiate the settings with the port number.
		StartupSettings settings = new StartupSettings(port, 1,
				new GossipSettings(gossipInterval));
		settings.setForgiveFailedCount(forgiveFailedCount);
		System.out.print("Config-members [");
		MemberManager memberManager = new MemberManager();
		ArrayList<GossipMember> gossipMembers = memberManager.ReadRemoteMemberList();
		for(GossipMember member: gossipMembers)
		{
			settings.addGossipMember(member);
		}
		return settings;
	}

}
